<!DOCTYPE html>
<html lang="zh-cn">
<head>
    <meta charset="UTF-8">
    <title>新增页面</title>
    <link rel="stylesheet" href="/app/admin/component/pear/css/pear.css" />
    <link rel="stylesheet" href="/app/admin/admin/css/reset.css" />
</head>
<body>

<form class="layui-form" action="">

    <div class="mainBox">
        <div class="main-container mr-5">

            <div class="layui-form-item">
                <label class="layui-form-label required">设备所属类型</label>
                <div class="layui-input-block">
                    <div name="device_type_id" id="device_type_id" required value="1" ></div>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label required">文件</label>
                <div class="layui-input-block">
                    <span></span>
                    <input type="text" style="display:none" name="url" value="" />
                    <button type="button" class="pear-btn pear-btn-primary pear-btn-sm" id="url">
                        <i class="layui-icon layui-icon-upload"></i>上传文件
                    </button>
                </div>
            </div>

        </div>
    </div>

</form>

<script src="/app/admin/component/layui/layui.js?v=2.8.12"></script>
<script src="/app/admin/component/pear/pear.js"></script>
<script src="/app/admin/admin/js/permission.js"></script>
<script>

    const IMPORT_EXCEL_URL = "/app/admin/iot-device/import_excel";

    // 字段 文件 url
    layui.use(["upload", "layer", "popup", "util"], function() {
        let input = layui.$("#url").prev();
        input.prev().html(layui.util.escape(input.val()));
        layui.upload.render({
            elem: "#url",
            accept: "file",
            data: {category: function () {
                    return layui.$('input[name="device_type_id"]').val();
                }},
            url: IMPORT_EXCEL_URL,
            field: "__file__",
            done: function (res) {
                if (res.code) return layui.popup.failure(res.msg);
                parent.refreshTable();
                parent.layer.close(parent.layer.getFrameIndex(window.name));
            }
        });
    });

    // 字段 类别 category
    layui.use(["jquery", "xmSelect"], function() {
        layui.$.ajax({
            url: "/app/admin/iot-device-type/select?format=tree",
            dataType: "json",
            success: function (res) {
                let value = layui.$("#device_type_id").attr("value");
                let initValue = value ? value.split(",") : [];
                layui.xmSelect.render({
                    el: "#device_type_id",
                    name: "device_type_id",
                    initValue: initValue,
                    filterable: true,
                    data: res.data,
                    value: "1",
                    model: {"icon":"hidden","label":{"type":"text"}},
                    clickClose: true,
                    radio: true,
                    tree: {show:true,strict:false,clickCheck:true,clickExpand:false,expandedKeys:initValue},
                    layVerify: "required",
                });
                if (res.code) {
                    layui.popup.failure(res.msg);
                }
            }
        });
    });

</script>

</body>
</html>
